<?php
namespace App\Admin\Controllers\Traits\WxBureauTraits;

use App\Admin\Renderable\BannerRender;
use App\Admin\Renderable\IconRender;
use App\Jobs\System\SettingImgToJpgJob;
use App\Models\WxClause;
use App\Models\WxIcon;
use App\Models\WxSlideshow;
use App\Wen\Utils\AdminUtils;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\GatewayUtils;
use App\Wen\Utils\Settings;
use App\Wen\Utils\SiteUtils;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Form;
use Illuminate\Http\Request;

trait formBureauTrait{

    public function index_config(Content $content)
    {
        return $content->title('组局活动配置')
//            ->body($this->buildPreviewButton())
//            ->body($this->newline())
            ->body(
                <<<HTML
<div class="card">{$this->form_config()->render()}</div>
HTML
            );
    }
    protected function form_config(){
        $form = new Form();
        $form->confirm('您确定要提交表单吗？', 'content');
        $form->disableResetButton();

        $form->tab('基本', function (Form $form) {
            $form->html(function (){
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">一、订单分成</div>';
            }, '');
            $form->rate('app_bureau_order_division', '组局订单平台分成')->default(Settings::get('app_bureau_order_division', 1.0));
            $form->divider();
            $form->html(function (){
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">二、活动类型</div>';
            }, '');
            $form->list('app_bureau_tags','活动类型')->default(Settings::get('app_bureau_tags', []));
            $form->divider();
            $form->html(function (){
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">三、审核相关</div>';
            }, '');

            $form->radio('is_audit_bureau', '活动是否需要审核')->help('AI审核需要配置百度文字审核API，<a href="'.admin_url('settings/api').'" target="_blank">点击进入</a>')->options($this->audit_options)->default(Settings::get('is_audit_bureau', 0))->when('!=', 1, function (Form $form){
                $form->checkbox('audit_bureau_white_list', '组局活动审核白名单')->options(FieldUtils::userPermissions())->default(Settings::get('audit_bureau_white_list', ''));
            });

            $form->divider();

            $form->radio('is_audit_bureau_comment', '留言是否需要审核')->help('AI审核需要配置百度文字审核API，<a href="'.admin_url('settings/api').'" target="_blank">点击进入</a>')->options($this->audit_options)->default(Settings::get('is_audit_bureau_comment', 0))->when('!=', 1, function (Form $form){
                $form->checkbox('audit_bureau_comment_white_list', '留言审核白名单')->options(FieldUtils::userPermissions())->default(Settings::get('audit_bureau_comment_white_list', ''));
            });
            $form->divider();
            $form->html(function (){
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">四、通知相关</div>';
            }, '');
            $form->number('app_bureau_notice_before_start', '活动开始前x分钟通知')->help('最小为5')->default(Settings::get('app_bureau_notice_before_start', 20));
            $form->divider();
            $form->html(function (){
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">五、图片</div>';
            }, '');
            $form->image('app_bureau_club_price_bg', '价格底图')->help('<a href="https://img.mini.minisns.cn/2024/06/26/0/aadf56680a534ae75986f738aa35d40d.png" download="members_popup_poster.jpg" target="_blank" style="color: steelblue">点击下载默认图</a>')->url('files/uploads')->uniqueName()->autoUpload()->default(Settings::get('app_bureau_club_price_bg', 'https://img.mini.minisns.cn/2024/06/26/0/aadf56680a534ae75986f738aa35d40d.png'));
            $form->image('app_bureau_club_price_gray_bg', '价格灰图')->url('files/uploads')->uniqueName()->autoUpload()->help('<a href="https://img.mini.minisns.cn/2024/06/26/0/60fad64bb24e9effc4e7a08195f5b5a4.png" target="_blank">下载默认图</a>')->default(Settings::get('app_bureau_club_price_gray_bg', 'https://img.mini.minisns.cn/2024/06/26/0/60fad64bb24e9effc4e7a08195f5b5a4.png'));
        });

        $form->tab('俱乐部', function (Form $form) {
            $form->image('app_bureau_club_settle_in_bg', '申请俱乐部的背景')->help('<a href="https://img.mini.minisns.cn/2024/06/19/0/8a3a5a3b0eb323ab7747995d49809d6b.png" download="members_popup_poster.jpg" target="_blank" style="color: steelblue">点击下载默认图</a>')->url('files/uploads')->uniqueName()->autoUpload()->default(Settings::get('app_bureau_club_settle_in_bg', 'https://img.mini.minisns.cn/2024/06/19/0/8a3a5a3b0eb323ab7747995d49809d6b.png'));
            $form->image('app_bureau_club_default_avatar', '俱乐部默认头像')->url('files/uploads')->uniqueName()->autoUpload()->help('<a href="https://img.mini.minisns.cn/2024/06/15/0/c5481f075fbffbf4b0e303b71f604bdc.png" target="_blank">下载默认图</a>')->default(Settings::get('app_bureau_club_default_avatar', 'https://img.mini.minisns.cn/2024/06/15/0/c5481f075fbffbf4b0e303b71f604bdc.png'));
            $form->image('app_bureau_club_background_maps', '俱乐部默认背景图')->url('files/uploads')->uniqueName()->autoUpload()->help('<a href="https://img.mini.minisns.cn/2024/06/25/0/456d67727c0ae42645d08f68f181e301.jpg" target="_blank">下载默认图</a>')->default(Settings::get('app_bureau_club_background_maps', 'https://img.mini.minisns.cn/2024/06/25/0/456d67727c0ae42645d08f68f181e301.jpg'));
            $form->select('app_bureau_club_settle_in_clause', '《组局俱乐部产品服务协议》')->options(WxClause::pluck('title', 'id'))->help('<a href="'.admin_url('clause').'" target="_blank">进入条款管理</a>')->default(Settings::get('app_bureau_club_settle_in_clause', 0));
        });

        $form->tab('发布', function (Form $form) {
            $form->radio('app_bureau_push_force_bind_phone', '强制绑定手机号')->options([0=>'不强制', 1=>'强制'])->default(Settings::get('app_bureau_push_force_bind_phone', 0, true));
            $form->radio('app_bureau_push_force_real_name', '强制实名')->options([0=>'不强制', 1=>'强制'])->default(Settings::get('app_bureau_push_force_real_name', 0, true));
            $form->divider();
            global $__MINI_GLOBAL_TENANT_ID__;
            $form->radio('app_bureau_push_force_subscribe_mp', '强制关注公众号')->options([0=>'不强制', 1=>'强制'])->help($__MINI_GLOBAL_TENANT_ID__ > 0 ? '必须开启服务器地址' : '必须开启服务器地址，具体请去[全局配置-支付-微信-公众号]')->default(Settings::get('app_bureau_push_force_subscribe_mp', 0, true));
        });

        $form->tab('首页', function (Form $form) {

            $form->multipleSelectTable('app_bureau_header_icon', '头部左右侧')
                ->title('图标库')
                ->max(2)
                ->from(IconRender::make())
                ->model(WxIcon::class, 'id', 'name', 'selectTable:formBureauTrait:app_used_header_icon')
                ->help('添加两个，若把组局设置为tabbar，则可以显示， <a href="'.admin_url('settings/icons').'" target="_blank">管理图标库>></a><a href="https://doc.minisns.cn/doc/70/" target="_blank" style="color: red;">（ps：如何控制顺序?）&nbsp;</a>')
                ->default(Settings::get('app_bureau_header_icon', ''));

            $form->multipleSelectTable('app_bureau_index_banner', '轮播图')
                ->title('轮播图库')
                ->max(10)
                ->from(BannerRender::make())
                ->model(WxSlideshow::class, 'id', 'id', 'selectTable:formBureauTrait:app_bureau_index_banner')
                ->help('<a href="'.admin_url('slideshow').'" target="_blank">运营管理-轮播图中心</a><a href="https://doc.minisns.cn/doc/70/" target="_blank" style="color: red;">（ps：如何控制顺序?）&nbsp;</a>')->default(Settings::get('app_bureau_index_banner', ''));
        });
        $form->tab('分享', function (Form $form) {
            $form->divider();
            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">分享</div>';
            }, '');
            $form->text('app_bureau_index_share_title', '首页分享标题')->default(Settings::get('app_bureau_index_share_title', '超超好看的组局活动'));
            $form->image('app_bureau_index_share_image', '首页分享海报')->help('<a href="https://img.mini.minisns.cn/2024/06/19/0/24951408eb521601364b6f6a5ca7c02a.jpg" download="members_popup_poster.jpg" target="_blank" style="color: steelblue">点击下载默认图</a>')->url('files/uploads')->uniqueName()->autoUpload()->default(Settings::get('app_bureau_index_share_image', 'https://img.mini.minisns.cn/2024/06/19/0/24951408eb521601364b6f6a5ca7c02a.jpg'));
            $form->text('app_bureau_detail_share_title', '活动详情分享标题')->help('[bureau_title]将被替换成活动标题')->default(Settings::get('app_bureau_detail_share_title', '[bureau_title] - 组局活动'));
            $form->divider();
            $form->checkbox('app_bureau_social_share_types', '社交分享')->options([
                'refer' => '转发',
                'wechat' => '微信',
                'timeline' => '朋友圈',
                'qq' => 'QQ',
                'qzone' => 'QQ空间',
                'weibo' => '微博',
            ])->default(Settings::get('app_bureau_social_share_types', ['refer', 'wechat', 'timeline', 'qq', 'qzone', 'weibo']));
            $form->checkbox('app_bureau_social_system_types', '系统功能')->options([
                'showCode' => '生成海报',
                'edit' => '编辑',
                'copyUrl' => '复制链接',
                'report' => '举报',
            ])->default(Settings::get('app_bureau_social_system_types', ['showCode','edit', 'copyUrl', 'report']));
        });

        $form->tab('海报', function (Form $form) {
            $form->image('app_bureau_poster_bg', '背景图')->help('固定大小：780*1015px的png格式<a href="https://img.mini.minisns.cn/2024/10/19/0/4c3e93059d9907203a8ff7a14e4a4249.png" download="members_popup_poster.jpg" target="_blank" style="color: steelblue">点击下载默认图</a>')->url('files/uploads')->uniqueName()->autoUpload()->default(Settings::get('app_bureau_poster_bg', 'https://img.mini.minisns.cn/2024/10/19/0/4c3e93059d9907203a8ff7a14e4a4249.png'));
        });

        $form->action('bureau/action/config');
        return $form;
    }

    public function config(Request $request){
        SiteUtils::forget_config_data();

        $audit_bureau_white_list = $request->input('audit_bureau_white_list');
        $audit_bureau_white_list_ = [];
        if($audit_bureau_white_list){
            foreach ($audit_bureau_white_list as $i){
                if($i === null){
                    continue;
                }
                $audit_bureau_white_list_[] = (int)$i;
            }
        }

        Settings::set('audit_bureau_white_list', $audit_bureau_white_list_, true);

        $audit_bureau_comment_white_list = $request->input('audit_bureau_comment_white_list');
        $audit_bureau_comment_white_list_ = [];
        if($audit_bureau_comment_white_list){
            foreach ($audit_bureau_comment_white_list as $i){
                if($i === null){
                    continue;
                }
                $audit_bureau_comment_white_list_[] = (int)$i;
            }
        }

        Settings::set('audit_bureau_comment_white_list', $audit_bureau_comment_white_list_, true);

        $app_bureau_tags = AdminUtils::list_fiele_process($request->input('app_bureau_tags'));
        Settings::set('app_bureau_tags', $app_bureau_tags, true);

        $app_bureau_tags_ = json_decode($app_bureau_tags, true);
        $app_bureau_tags_arr = [];
        foreach ($app_bureau_tags_ as $key => $tag){
            $app_bureau_tags_arr[] = [
                'label' => $tag,
                'name' => $tag,
                'value' => $key
            ];
        }
        Settings::set('app_bureau_tags_', json_encode($app_bureau_tags_arr), true);

        Settings::set('app_bureau_order_division', _between_($request->input('app_bureau_order_division'), 0, 100), true);
        Settings::set('is_audit_bureau', $request->input('is_audit_bureau'), true);
        Settings::set('is_audit_bureau_comment', $request->input('is_audit_bureau_comment'), true);

        Settings::set('app_bureau_header_icon', AdminUtils::_multipleSelectTable_order_process( 'selectTable:formBureauTrait:app_bureau_header_icon', $request->input('app_bureau_header_icon') ), true);
        Settings::set('app_bureau_index_banner', AdminUtils::_multipleSelectTable_order_process('selectTable:formBureauTrait:app_bureau_index_banner', $request->input('app_bureau_index_banner')) , true);

        Settings::set('app_bureau_club_settle_in_clause', $request->input('app_bureau_club_settle_in_clause'), true);
        Settings::set('app_bureau_club_settle_in_bg', $request->input('app_bureau_club_settle_in_bg'), true);
        Settings::set('app_bureau_club_default_avatar', $request->input('app_bureau_club_default_avatar'), true);
        Settings::set('app_bureau_club_background_maps', $request->input('app_bureau_club_background_maps'), true);
        Settings::set('app_bureau_detail_share_title', $request->input('app_bureau_detail_share_title'), true);
        Settings::set('app_bureau_index_share_title', $request->input('app_bureau_index_share_title'), true);
        Settings::set('app_bureau_index_share_image', $request->input('app_bureau_index_share_image'), true);

        Settings::set('app_bureau_poster_bg', $request->input('app_bureau_poster_bg'), true);
        Settings::set('app_bureau_notice_before_start', _between_($request->input('app_bureau_notice_before_start'), 5, 1000), true);
//        Settings::set('app_bureau_notice_end_after', _between_($request->input('app_bureau_notice_end_after'), 10, 1000), true);

        Settings::set('app_bureau_push_force_bind_phone', $request->input('app_bureau_push_force_bind_phone'), true);
        Settings::set('app_bureau_push_force_real_name', $request->input('app_bureau_push_force_real_name'), true);
        Settings::set('app_bureau_push_force_subscribe_mp', $request->input('app_bureau_push_force_subscribe_mp'), true);


        Settings::set('app_bureau_club_price_bg', $request->input('app_bureau_club_price_bg'), true);
        Settings::set('app_bureau_club_price_gray_bg', $request->input('app_bureau_club_price_gray_bg'), true);
        Settings::set('app_bureau_social_share_types', $request->input('app_bureau_social_share_types'), true);
        Settings::set('app_bureau_social_system_types', $request->input('app_bureau_social_system_types'), true);

        SettingImgToJpgJob::dispatch(['app_bureau_index_share_image']);

        $__global_config_version__ = Settings::get('__global_config_version__', 1) + 1;
        Settings::set('__global_config_version__', $__global_config_version__);
        GatewayUtils::success('all', 13, [ 'version' => $__global_config_version__, 'file' => basename(__FILE__) ]);
        return $this->response()->success('成功！');
    }

}
